/*
 * Copyright (C) 2013 AndXmppSDK Project <hljdrl@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.drl.xmpp;

import java.util.Date;

import org.jivesoftware.smack.packet.PacketExtension;
import org.jivesoftware.smack.packet.XMPPError;
import org.jivesoftware.smackx.packet.DelayInformation;

import android.os.Parcel;
import android.os.Parcelable;

/**
 * 
 * 消息-聊天的消息对象
 * 
 * @author hljdrl@gmail.com
 * 
 */
public final class TMessage implements Parcelable {
	 /** Chat message type. */
    public static final int MSG_TYPE_CHAT = 20;
    
    /** 申请加好友的消息类型 */
    public static final int MSG_TYPE_SUBSCRIBE_CHAT = 21;
    
    /** Group chat message type. */
    public static final int MSG_TYPE_GROUP_CHAT = 30;

    /** Error message type. */
    public static final int MSG_TYPE_ERROR = 40;

    /** Informational message type. */
    public static final int MSG_TYPE_INFO = 50;
	public static final Parcelable.Creator<TMessage> CREATOR = new Parcelable.Creator<TMessage>() {

		@Override
		public TMessage createFromParcel(Parcel source) {
			return new TMessage(source);
		}

		@Override
		public TMessage[] newArray(int size) {
			return new TMessage[size];
		}
	};
	public  Long    id;
	private String mPacketID="";
	private String mFrom;
	private String mTo;
	private String mBody;
	private String mSubject;
	private String mThread;
	private Date mTimestamp;
	private int mType;
	private boolean isRead;
	public TMessage(Parcel in) {
		id = in.readLong();
		mType = in.readInt();
		mTo = in.readString();
		mBody = in.readString();
		setSubject(in.readString());
		setThread(in.readString());
		mFrom = in.readString();
		mTimestamp = new Date(in.readLong());
		setPacketID(in.readString());
		isRead = in.readInt()!=0;
	}
	
	
	
	public TMessage(String to) {
		this(to, MSG_TYPE_CHAT);
	}

	public TMessage(final String to, final int type) {
		this.mTo = to;
		this.mType = type;
		mBody = "";
		setSubject("");
		setThread("");
		mFrom = "";
		mTimestamp = new Date();
	}
	
	public TMessage(org.jivesoftware.smack.packet.Message smackMsg) {
	
		this(smackMsg.getTo());
		switch (smackMsg.getType()) {
		case chat:
			mType = MSG_TYPE_CHAT;
			break;
		case groupchat:
			mType = MSG_TYPE_GROUP_CHAT;
			break;
		case normal:
			mType = MSG_TYPE_INFO;
			break;
		case error:
			mType = MSG_TYPE_ERROR;
			break;
		default:
			mType = MSG_TYPE_INFO;
			break;
		}
		this.mFrom = smackMsg.getFrom();
		// TODO better handling of error messages
		if (mType == MSG_TYPE_ERROR) {
			XMPPError er = smackMsg.getError();
			String msg = er.getMessage();
			if (msg != null)
				mBody = msg;
			else
				mBody = er.getCondition();
		} else {
			mBody = smackMsg.getBody();
			setSubject(smackMsg.getSubject());
			setThread(smackMsg.getThread());
		}
		PacketExtension pTime = smackMsg
				.getExtension("delay", "urn:xmpp:delay");
		if (pTime instanceof DelayInformation) {
			mTimestamp = ((DelayInformation) pTime).getStamp();
		} else {
			mTimestamp = new Date();
		}
		setPacketID(smackMsg.getPacketID());
		if(getPacketID()==null){
			setPacketID(smackMsg.getThread());
		}
	
	}

	public String getFrom() {
		return mFrom;
	}

	public void setFrom(String form) {
		this.mFrom = form;
	}

	public String getTo() {
		return mTo;
	}

	public void setTo(String to) {
		this.mTo = to;
	}

	public String getBody() {
		return mBody;
	}

	public void setBody(String body) {
		this.mBody = body;
	}

	public int getType() {
		return mType;
	}

	public void setType(int type) {
		this.mType = type;
	}
	@Override
	public int describeContents() {
		return 0;
	}
	@Override
	public void writeToParcel(Parcel dest, int flags) {
		dest.writeLong(id);
		dest.writeInt(mType);
		dest.writeString(mTo);
		dest.writeString(mBody);
		dest.writeString(getSubject());
		dest.writeString(getThread());
		dest.writeString(mFrom);
		dest.writeLong(mTimestamp.getTime());
		dest.writeString(getPacketID());
		dest.writeInt(isRead?1:0);
	}
	public Date getTimestamp() {
		return mTimestamp;
	}
	public void setTimestamp(Date mTimestamp) {
		this.mTimestamp = mTimestamp;
	}

	public String getPacketID() {
		return mPacketID;
	}

	public void setPacketID(String mPacketID) {
		this.mPacketID = mPacketID;
	}

	public String getThread() {
		return mThread;
	}

	public void setThread(String mThread) {
		this.mThread = mThread;
	}

	public String getSubject() {
		return mSubject;
	}

	public void setSubject(String mSubject) {
		this.mSubject = mSubject;
	}



	public boolean isRead() {
		return isRead;
	}



	public void setRead(boolean isRead) {
		this.isRead = isRead;
	}

}
